# -*-coding:utf-8 -*-
import os
from urllib.parse import parse_qsl

from sanic import Sanic
from sanic.exceptions import NotFound
from sanic.response import json

from .xz_handler.gps import GPSHandler
from .xz_handler.gps_mongo import GPSMongoHandler
from .xz_handler.image import ImageHandler
from .xz_handler.terminal import TerminalHandler
from .xz_handler.video import VideoHandler

app = Sanic()


video_handler = VideoHandler()
gps_handler = GPSHandler()
img_handler = ImageHandler()
terminal_handler = TerminalHandler()
gps_mongo = GPSMongoHandler()


def save_request(request):
    with open(os.path.basename(request.path), 'ab') as wf:
        wf.write(request.body)


@app.route("/app_terminal/gps_info.json",  methods=["POST", 'GET'])
async def gps_info(request):
    gps_handler.handle(request.json)
    gps_mongo.handle(request.json)
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/app_video/del_history_video.json",  methods=["POST", 'GET'])
async def del_history_video(request):
    # body = request.body.decode('utf-8')
    # save_request(request)
    # video_handler.del_history_video(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/app_video/add_history_video.json",  methods=["POST", 'GET'])
async def add_history_video(request):
    body = request.body.decode('utf-8')
    video_handler.add_history_video(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/app_video/add_history_vedio.json",  methods=["POST", 'GET'])
async def add_history_vedio(request):
    body = request.body.decode('utf-8')
    video_handler.add_history_video(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/warn/video_upload.json",  methods=["POST", 'GET'])
async def video_upload(request):
    res = video_handler.video_upload(request.files, request.form)
    return json({'code': 2000, 'msg': '操作成功！', 'result': {'urlName': res['media_uri']}})


@app.route("/face/contrast_warn_image.json",  methods=["POST", 'GET'])
async def contrast_warn_image(request):
    body = request.body.decode('utf-8')
    img_handler.contrast_warn_image(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/warn/add_warn.json", methods=["POST", 'GET'])
async def add_warn(request):
    body = request.body.decode('utf-8')
    img_handler.add_warn(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/app_terminal/terminal_register.json", methods=["POST", 'GET'])
async def terminal_register(request):
    body = request.body.decode('utf-8')
    terminal_handler.register(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/app_terminal/terinal_register.json", methods=["POST", 'GET'])
async def terinal_register(request):
    body = request.body.decode('utf-8')
    terminal_handler.register(dict(parse_qsl(body)))
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


# @app.route("/app_terminal/vehicle_id.json", methods=['GET'])
# async def vehicle_id(request):
#     body = request.body.decode('utf-8')
#     terminal_handler.register(dict(parse_qsl(body)))
#     return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.route("/log/tersorflow_log.json",  methods=["POST", 'GET'])
async def tersorflow_log(request):
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


@app.exception(NotFound)
async def ignore_404s(request, exception):
    print(request.path, request.body)
    save_request(request)
    return json({'code': 2000, 'msg': '操作成功！', 'result': None})


def run():
    app.run(host="0.0.0.0", port=8087, debug=False, access_log=False)
